package mbp.base;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import mbp.core.JarFolder;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Grupuje podstawowe ustawienia platformy
 * @author Wojciech Wilanski
 *
 */
public class Settings 
{
	private static File fileSettings;
	private static Document docSettings;
	
	public static String managerName = "manager";
	public static String storageName = "mbpstorage";
	public static String registerName = "mbpregister";
	public static String snifferName = "mbpsniffer";
	public static String packageName = "mbp.agents";
	public static TimeUnit timeUnit = TimeUnit.SECONDS;
	
	public static boolean isSniffer = true;
	public static boolean sniffRegister = true;
	public static boolean sniffStorage = true;
	public static boolean sniffClient = false;
	public static boolean sniffTask = true;
	public static boolean sniffBase = true;
	public static boolean sniffServer = false;
	
	public Settings() {}
	
	public static int getMultiplier()
	{
		int multiplier = 1;
		
		//Ustawienie mnoznika dla jednostek czasu
		switch(timeUnit)
		{
		case MINUTES:
			multiplier = 60;
			break;
		case HOURS:
			multiplier = 60 * 60;
			break;
		case DAYS:
			multiplier = 60 * 60 * 24;
			break;
		}
		
		return multiplier;
	}
	
	public static void loadSettings()
	{
		JarFolder jf = new JarFolder();
		String xmlSettings = jf.getJarFolder() + "conf" + File.separatorChar + "Settings.xml";
		fileSettings = new File(xmlSettings);		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		try {
			DocumentBuilder db1 = dbf.newDocumentBuilder();
			docSettings = db1.parse(fileSettings);
			
			docSettings.getDocumentElement().normalize();
			
			NodeList storageList = docSettings.getElementsByTagName("storagename");
			NodeList registerList = docSettings.getElementsByTagName("registername");
			NodeList snifferList = docSettings.getElementsByTagName("sniffername");
			NodeList packageList = docSettings.getElementsByTagName("packagename");
			NodeList timeList = docSettings.getElementsByTagName("timeunit");
			NodeList issnifferList = docSettings.getElementsByTagName("issniffer");
			NodeList sniffregisterList = docSettings.getElementsByTagName("sniffregister");
			NodeList sniffstorageList = docSettings.getElementsByTagName("sniffstorage");
			NodeList sniffclientList = docSettings.getElementsByTagName("sniffclient");
			NodeList snifftaskList = docSettings.getElementsByTagName("snifftask");
			NodeList sniffbaseList = docSettings.getElementsByTagName("sniffbase");
			NodeList sniffserverList = docSettings.getElementsByTagName("sniffserver");
			
			if(storageList.getLength()==1)
			{
				Element element = (Element)storageList.item(0);
				storageName = element.getAttribute("value");
			}
			
			if(registerList.getLength()==1)
			{
				Element element = (Element)registerList.item(0);
				registerName = element.getAttribute("value");
			}
			
			if(snifferList.getLength()==1)
			{
				Element element = (Element)snifferList.item(0);
				snifferName = element.getAttribute("value");
			}
			
			if(packageList.getLength()==1)
			{
				Element element = (Element)packageList.item(0);
				packageName = element.getAttribute("value");
			}
			
			if(timeList.getLength()==1)
			{
				Element element = (Element)timeList.item(0);
				timeUnit = TimeUnit.valueOf( element.getAttribute("value") );
			}
			
			if(issnifferList.getLength()==1)
			{
				Element element = (Element)issnifferList.item(0);
				isSniffer = Boolean.parseBoolean( element.getAttribute("value") );
			}
			
			if(sniffregisterList.getLength()==1)
			{
				Element element = (Element)sniffregisterList.item(0);
				sniffRegister = Boolean.parseBoolean( element.getAttribute("value") );
			}
			
			if(sniffstorageList.getLength()==1)
			{
				Element element = (Element)sniffstorageList.item(0);
				sniffStorage = Boolean.parseBoolean( element.getAttribute("value") );
			}
			
			if(sniffclientList.getLength()==1)
			{
				Element element = (Element)sniffclientList.item(0);
				sniffClient = Boolean.parseBoolean( element.getAttribute("value") );
			}
			
			if(snifftaskList.getLength()==1)
			{
				Element element = (Element)snifftaskList.item(0);
				sniffTask = Boolean.parseBoolean( element.getAttribute("value") );
			}
			
			if(sniffbaseList.getLength()==1)
			{
				Element element = (Element)sniffbaseList.item(0);
				sniffBase = Boolean.parseBoolean( element.getAttribute("value") );
			}
			
			if(sniffserverList.getLength()==1)
			{
				Element element = (Element)sniffserverList.item(0);
				sniffServer = Boolean.parseBoolean( element.getAttribute("value") );
			}
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveSettings()
	{
		
	}
}
